
SCROLL_VIEWPORT_HEIGHT = 20

; Params

dtm_screen_adress	word	0
dtm_bbuffer_adress	word	0
dtm_tilemap_width	byte	0
dtm_tilemap_height	byte	0
dtm_tiles			word	0		; Adress of the tiles data
dtm_tilemap			word	0		; Adress of the tilemap
dtm_scrollx			byte	2
dtm_scrolly			byte	0

; Varuiables

dtm_screenx			byte	0
dtm_screeny			byte	0
dtm_scrolledx		byte	0
dtm_scrolledy		byte	0
dtm_tilex			byte	0
dtm_tiley			byte	0
dtm_lasttilex		byte	0
dtm_lasttiley		byte	0
dtm_tilesubx		byte	0
dtm_tilesuby		byte	0
dtm_currenttile		word	0

drawtilemap

	; Startup variables
	lda #255
	sta dtm_lasttilex
	sta dtm_lasttiley
	
	; Set screen pointer
	lda dtm_screen_adress
	sta PGZERO_VECTOR_B
	lda dtm_screen_adress+1
	sta PGZERO_VECTOR_B+1
	
	lda #0
	sta dtm_screeny
	
dtm_screeny_loop	

	lda #0
	sta dtm_screenx
	ldy #0
	
dtm_screenx_loop

	; Draw a character
	jsr dtm_findcharacter
	
	ldy dtm_screenx
	sta (PGZERO_VECTOR_B),y
	
	; Increase screenx and check if we reached the limit
	inc dtm_screenx
	lda dtm_screenx
	cmp #40
	beq dtm_screenx_loop_break
	jmp dtm_screenx_loop
	
dtm_screenx_loop_break	

	; Increase screen memory pointer to next row
	lda PGZERO_VECTOR_B
	clc
	adc #40
	sta PGZERO_VECTOR_B
	lda PGZERO_VECTOR_B+1
	adc #0
	sta PGZERO_VECTOR_B+1
	
	; Increase screen y and check if we reached the limit
	inc dtm_screeny
	lda dtm_screeny
	cmp #SCROLL_VIEWPORT_HEIGHT
	beq dtm_screeny_loop_break
	jmp dtm_screeny_loop
	
dtm_screeny_loop_break	
		
	rts
	
;===========================================================

drawtilemaplastcolumn

	; Startup variables
	lda #255
	sta dtm_lasttilex
	sta dtm_lasttiley
	
	; Set screen pointer
	lda dtm_screen_adress
	sta PGZERO_VECTOR_B
	lda dtm_screen_adress+1
	sta PGZERO_VECTOR_B+1
	
	lda #0
	sta dtm_screeny
	
dtmlc_screeny_loop	

	lda #39
	sta dtm_screenx
	ldy #0
	
dtmlc_screenx_loop

	; Draw a character
	jsr dtm_findcharacter
	
	ldy dtm_screenx
	sta (PGZERO_VECTOR_B),y
	
	; Increase screenx and check if we reached the limit
	inc dtm_screenx
	lda dtm_screenx
	cmp #40
	beq dtmlc_screenx_loop_break
	jmp dtmlc_screenx_loop
	
dtmlc_screenx_loop_break	

	; Increase screen memory pointer to next row
	lda PGZERO_VECTOR_B
	clc
	adc #40
	sta PGZERO_VECTOR_B
	lda PGZERO_VECTOR_B+1
	adc #0
	sta PGZERO_VECTOR_B+1
	
	; Increase screen y and check if we reached the limit
	inc dtm_screeny
	lda dtm_screeny
	cmp #SCROLL_VIEWPORT_HEIGHT
	beq dtmlc_screeny_loop_break
	jmp dtmlc_screeny_loop
	
dtmlc_screeny_loop_break	
		
	rts
	
;===========================================================
; dtm_findcharacter

dtm_findcharacter

	; scrolledx = screenx + scrollx
	lda dtm_screenx
	clc
	adc dtm_scrollx
	sta dtm_scrolledx
	
	; scrolledx = screeny + scrolly
	lda dtm_screeny
	clc
	adc dtm_scrolly
	sta dtm_scrolledy
	
	; tilesubx = scrolledx % 4
	lda dtm_scrolledx
	and #%00000011
	sta dtm_tilesubx
	
	; tilesubx = scrolledy % 4
	lda dtm_scrolledy
	and #%00000011
	sta dtm_tilesuby
	
	; tilex = scrolledx / 4
	lda dtm_scrolledx
	lsr
	lsr
	sta dtm_tilex
		
	; tiley = scrolledy / 4
	lda dtm_scrolledy
	lsr
	lsr
	sta dtm_tiley
	
	; Check if we can skip the finding tile adress part:
	lda dtm_tilex
	cmp dtm_lasttilex
	bne dtm_need_update_tile_adress
	
	lda dtm_tiley
	cmp dtm_lasttiley
	bne dtm_need_update_tile_adress
	
	jmp dtm_tile_adress_ok
	
dtm_need_update_tile_adress	
	
	; Find the tile value we are in (&tilemap  + (tilemapwidth*tiley) + tilex
	lda dtm_tilemap
	sta PGZERO_VECTOR_A
	lda dtm_tilemap+1
	sta PGZERO_VECTOR_A+1
	
	; Add tilemapwidth tiley times
	ldx dtm_tiley
	beq dtm_fc_loopfindtiley_end		; Skip if tiley == 0

dtm_fc_loopfindtiley

	lda PGZERO_VECTOR_A
	clc
	adc dtm_tilemap_width
	sta PGZERO_VECTOR_A
	lda PGZERO_VECTOR_A+1
	adc #0
	sta PGZERO_VECTOR_A+1
		
	dex
	bne dtm_fc_loopfindtiley
	
dtm_fc_loopfindtiley_end
	
	; Add tilex
	lda PGZERO_VECTOR_A
	clc
	adc dtm_tilex
	sta PGZERO_VECTOR_A
	lda PGZERO_VECTOR_A+1
	adc #0
	sta PGZERO_VECTOR_A+1
	
	; Now we have the tile id
	ldy #0
	lda (PGZERO_VECTOR_A),y
	sta dtm_currenttile
	lda #0
	sta dtm_currenttile+1
		
	; We are going to make the PGZERO_A vector point to the current tile data: &tiles + 16 * currenttile
	lda dtm_tiles
	sta PGZERO_VECTOR_A
	lda dtm_tiles+1
	sta PGZERO_VECTOR_A+1
	
	; Calc currenttile * 16 
	lda dtm_currenttile
	asl
	sta dtm_currenttile
	lda dtm_currenttile+1
	rol
	sta dtm_currenttile+1
	
	lda dtm_currenttile
	asl
	sta dtm_currenttile
	lda dtm_currenttile+1
	rol
	sta dtm_currenttile+1
	
	lda dtm_currenttile
	asl
	sta dtm_currenttile
	lda dtm_currenttile+1
	rol
	sta dtm_currenttile+1
	
	lda dtm_currenttile
	asl
	sta dtm_currenttile
	lda dtm_currenttile+1
	rol
	sta dtm_currenttile+1
	
	; Add the offset to the vector
	lda PGZERO_VECTOR_A
	clc
	adc dtm_currenttile
	sta PGZERO_VECTOR_A
	lda PGZERO_VECTOR_A+1
	adc dtm_currenttile+1
	sta PGZERO_VECTOR_A+1
	
dtm_tile_adress_ok

	; Store this call's tilex and tiley
	lda dtm_tilex
	sta dtm_lasttilex
	
	lda dtm_tiley
	sta dtm_lasttiley
	
	; We calc the offset inside the tile, tilesuby*4 + tilesubx
	lda dtm_tilesuby
	asl
	asl
	clc
	adc dtm_tilesubx
	tay					; And transfer it to Y
		
	; Load the character into the accumulator
	lda (PGZERO_VECTOR_A),y
		
	rts
	
;====================================================

shifttilemapleft

	lda dtm_screen_adress
	sta PGZERO_VECTOR_A
	lda dtm_screen_adress+1
	sta PGZERO_VECTOR_A+1

	lda dtm_bbuffer_adress
	sta PGZERO_VECTOR_B
	lda dtm_bbuffer_adress+1
	sta PGZERO_VECTOR_B+1

	ldx #SCROLL_VIEWPORT_HEIGHT
	
stl_loopy

	ldy #0
	
stl_loopx
	
	iny
	lda (PGZERO_VECTOR_A),y
	dey
	sta (PGZERO_VECTOR_B),y
	iny
	cpy #39
	bne stl_loopx
	
	; Increase screen memory pointer to next row
	lda PGZERO_VECTOR_B
	clc
	adc #40
	sta PGZERO_VECTOR_B
	lda PGZERO_VECTOR_B+1
	adc #0
	sta PGZERO_VECTOR_B+1
	
	; Increase screen memory pointer to next row
	lda PGZERO_VECTOR_A
	clc
	adc #40
	sta PGZERO_VECTOR_A
	lda PGZERO_VECTOR_A+1
	adc #0
	sta PGZERO_VECTOR_A+1
	
	dex
	bne stl_loopy
		
	rts